using UnityEngine;
using System.Linq;
using System;
using System.Collections;
using System.Collections.Generic;

public class PlayerController : MonoBehaviour
{
    private Animator animatorWeapon;
    private AudioSource audioSource;
    private CharacterController characterController;
    private Light flashLight;
    private WeaponController weaponController;

    public float movementSpeed = 5;
    public float walkSpeed = 5;
    public float runningSpeed = 10;
    [Header("鼠标灵敏度")]
    public float mouseSensitivity = 100;
    private float rotationX = 0;
    private float rotationY = 0;
    private bool gunReloading = false;

    #region 攻击
    private float shootTime;
    #endregion

    #region 声音
    private AudioClip soundWalk;
    #endregion

    /// <summary>
    /// 玩家状态
    /// </summary>
    public PlayerStatus Status { get; set; }
    /// <summary>
    /// 玩家当前动作
    /// </summary>
    public ActionStatus CurrentAction { get; set; }

    void Awake()
    {
        this.animatorWeapon = FindFirstObjectByType<Animator>();
        this.audioSource = GetComponent<AudioSource>();
        this.weaponController = FindFirstObjectByType<WeaponController>();
        Cursor.lockState = CursorLockMode.Locked;
        characterController = GetComponent<CharacterController>();
        this.flashLight = GameObject.FindGameObjectWithTag(Defines.Flashlight).GetComponent<Light>();
        flashLight.enabled = false;

        this.soundWalk = Resources.Load<AudioClip>("AudioClips/Move");
    }

    void Start()
    {
        
    }

    void Update()
    {
        this.SystemSettings();
        this.UtilityItems();
        this.Architectures();

        switch (this.Status)
        {
            case PlayerStatus.Normal:

                this.Movement();
                this.Rotation();
                break;
            case PlayerStatus.Locked:
                this.Rotation();
                break;
        }

        if (this.CurrentAction != ActionStatus.Run
            && !this.weaponController.Reloading)
        {
            /* 攻击
               跑步和换弹时不可攻击
             */
            this.Attack();
        }
    }

    private void SystemSettings()
    {
        /* 退出游戏 */

        if (Input.GetKeyDown(KeyCode.Escape))
        {
            Application.Quit();
        }
    }

    private void Movement()
    {
        var horizatal = Input.GetAxis(Defines.Horizontal);
        var vertical = Input.GetAxis(Defines.Vertical);
        // var position = new Vector3(horizatal * this.movementSpeed * Time.deltaTime,
        //                             0,
        //                             vertical * this.movementSpeed * Time.deltaTime);
        // characterController.Move(position);
        characterController.Move((horizatal * transform.right + vertical * transform.forward) * movementSpeed * Time.deltaTime);
        characterController.Move(Vector3.up * Physics.gravity.y * Time.deltaTime);

        if (horizatal == 0 && vertical == 0)
        {
            this.CurrentAction = ActionStatus.Idle;
            if (this.audioSource.isPlaying)
            {
                this.audioSource.Stop();
            }
        }
        else
        {
            if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
            {
                this.CurrentAction = ActionStatus.Run;
                this.movementSpeed = this.runningSpeed;
            }
            else
            {
                this.CurrentAction = ActionStatus.Walk;
                this.movementSpeed = this.walkSpeed;
            }
            if(!this.audioSource.isPlaying)
            {
                this.audioSource.Play();
            }
        }
    }
    
    private void Rotation()
    {
        var mouseX = Input.GetAxis(Defines.MouseX);
        var mouseY = Input.GetAxis(Defines.MouseY);
        rotationX -= mouseY * mouseSensitivity * Time.deltaTime;
        rotationY += mouseX * mouseSensitivity * Time.deltaTime;
        // var rotation = new Vector3(mouseY * rotationSpeed * Time.deltaTime,
        //                             mouseX * rotationSpeed * Time.deltaTime,
        //                             0);
        transform.eulerAngles = new Vector3(rotationX, rotationY, 0);
    }

    private void UtilityItems()
    {
        /* 手电筒 */
        if (Input.GetKeyDown(KeyCode.L))
        {
            flashLight.enabled = !flashLight.enabled;
        }

        /* 武器 */

        if (Input.GetKeyDown(KeyCode.R))
        {
            this.weaponController.GunReload();
        }

        if (!this.weaponController.Reloading)
        {
            this.animatorWeapon.SetFloat(Defines.ActionStatus, (float)this.CurrentAction);
        }
    }

    private void Architectures()
    {
    }

    private void Attack()
    {
        switch (this.weaponController.CurrentWeapon)
        {
            case WeaponType.Gun:
                if (Input.GetMouseButtonDown(0))
                {
                    /* 点射 */
                    this.weaponController.Shoot();
                }
                else if (Input.GetMouseButton(0))
                {
                    /* 自动射击 */
                    this.weaponController.Shoot();
                }
                break;
        }
    }

}
